package com.zyh.flink.day03;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class AggregatingStateTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> hadoop10 = environment.socketTextStream("hadoop10", 9999);
        KeyedStream<Tuple2<String, Integer>, String> keyedStream = hadoop10.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s) throws Exception {
                String[] ss = s.split("\\s+");

                return Tuple2.of(ss[0], Integer.valueOf(ss[1]));
            }
        }).keyBy(t -> t.f0);

        SingleOutputStreamOperator<Tuple2<String, Double>> result = keyedStream.map(new MyAggregatingState());

        result.print();

        environment.execute("AggregatingStateJob");
    }
}
class MyAggregatingState extends RichMapFunction<Tuple2<String,Integer>,Tuple2<String,Double>>{
    //状态的IN,OUT泛型是添加到状态中的元素类型和返回的元素类型
    private AggregatingState<Integer,Double> aggregatingState;

    @Override
    public void open(Configuration parameters) throws Exception {
        RuntimeContext ctx = getRuntimeContext();
        AggregateFunction<Integer, Tuple2<Double, Integer>, Double> aggregateFunction = new AggregateFunction<Integer, Tuple2<Double, Integer>, Double>() {
            /*
            * 用于返回中间结果的初始值
            * */
            @Override
            public Tuple2<Double, Integer> createAccumulator() {
                return Tuple2.of(0.0,0);
            }
            /*
            * value:添加到状态中的一笔消费金额
            * accumulator:中间结果
            * */
            @Override
            public Tuple2<Double, Integer> add(Integer value, Tuple2<Double, Integer> accumulator) {
                return Tuple2.of(accumulator.f0,accumulator.f1+1);
            }
            /*
            * 从中间结果中获取最终结果的方法
            * */
            @Override
            public Double getResult(Tuple2<Double, Integer> accumulator) {
                return accumulator.f0/accumulator.f1;
            }
            /*
            * 合并
            * */
            @Override
            public Tuple2<Double, Integer> merge(Tuple2<Double, Integer> a, Tuple2<Double, Integer> b) {
                return Tuple2.of(a.f0+b.f0,a.f1+b.f1);
            }
        };
        AggregatingStateDescriptor<Integer, Tuple2<Double, Integer>, Double> asd = new AggregatingStateDescriptor<>("avg", aggregateFunction, Types.TUPLE(Types.DOUBLE, Types.INT));
        this.aggregatingState = ctx.getAggregatingState(asd);
    }

    /*
    *
    * */
    @Override
    public Tuple2<String, Double> map(Tuple2<String, Integer> value) throws Exception {
        //将订单金额添加到状态中
        aggregatingState.add(value.f1);

        //从状态中获取到平均金额
        Double avg = aggregatingState.get();
        return Tuple2.of(value.f0,avg);
    }
}